package com.zhengsr.drawbroad.utils;

import com.zhengsr.drawbroad.data.BaseData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2018/1/7.
 * csdn: http://blog.csdn.net/u011418943
 */

public class CommandUtils {

    private List<BaseData> mUndoBaseDatas = new ArrayList<>();
    private List<BaseData> mRedoBaseDatas = new ArrayList<>();

    private CommandUtils(){}

    public static CommandUtils sCommandUtils;
    public static CommandUtils getInstance(){
        if (sCommandUtils == null){
            synchronized (CommandUtils.class){
                if (sCommandUtils == null){
                    sCommandUtils = new CommandUtils();
                }
            }
        }
        return sCommandUtils;
    }

    public void add(BaseData baseData){
        mUndoBaseDatas.add(baseData);
    }

    public void undo(){
        if (!mUndoBaseDatas.isEmpty()){
            int count = mUndoBaseDatas.size();
            BaseData data = mUndoBaseDatas.get(count - 1);
            mUndoBaseDatas.remove(count - 1);
            mRedoBaseDatas.add(data);
        }
    }

    public void redo(){
        if (!mRedoBaseDatas.isEmpty()){
            int count = mRedoBaseDatas.size();
            BaseData data = mRedoBaseDatas.get(count - 1);
            mRedoBaseDatas.remove(count - 1);
            mUndoBaseDatas.add(data);
        }
    }

    public List<BaseData> getBaseDatas(){
        return mUndoBaseDatas;
    }

    public void clear() {
        mUndoBaseDatas.clear();
        mRedoBaseDatas.clear();
    }
}
